import fileBox from 'file-box';
import puppeteer from 'puppeteer';
import * as devices from "puppeteer/DeviceDescriptors";

// const iPhoneX = devices["iPhone X"];

async function screenshotImg(msg:any, imgText:any) {
    const browser = await puppeteer.launch({
        defaultViewport: {
            width: 1440,
            height: 780
        },
        args: ['--start-fullscreen']
    }); //生成browser实例
    const page = await browser.newPage();
    try{
        const imgUrl = imgText || "https://pic.sogou.com/pics?query=%C3%C0%C5%AE&di=2&_asf=pic.sogou.com&w=05009900"
        await page.goto(imgUrl);  //跳转到 https://example.com/
        await page.screenshot({                   //生成图片
        path: './static/img/example.png',
        type: 'png'
        });
        await browser.close();

        const fileImg = fileBox.fromFile('./static/img/example.png');
        console.log('图片生成成功');
        await msg.say(fileImg);
    }catch(err) {
        console.log(err);
    }
}

export default screenshotImg;
